package com.ekin.attendance.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ekin.attendance.model.entity.Leave;
import com.ekin.attendance.model.form.LeaveForm;
import com.ekin.attendance.model.query.LeavePageQuery;
import com.ekin.attendance.model.vo.LeavePageVO;
import com.ekin.attendance.service.LeaveService;
import com.ekin.common.annotation.RepeatSubmit;
import com.ekin.common.result.PageResult;
import com.ekin.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 请假管理控制层
 * @author Ekin
 * @date 2021/11/05
 */
@Tag(name = "01.请假管理接口")
@RestController
@RequestMapping("/api/attendance/leaves")
@RequiredArgsConstructor
public class LeaveController {
    private final LeaveService leaveService;
    //---------------------------------------------------
    // 请假相关接口
    //---------------------------------------------------
    @Operation(summary = "请假单分页列表")
    @GetMapping("/page")
    public PageResult<LeavePageVO> getLeavePage(LeavePageQuery queryParams)
    {
        Page<LeavePageVO> result = leaveService.getLeavePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "请假表单数据")
    @GetMapping("/{id}/form")
    public Result<LeaveForm> getLeaveForm(
            @Parameter(description = "请假ID") @PathVariable Long id
    ) {
        LeaveForm formData = leaveService.getLeaveForm(id);
        return Result.success(formData);
    }
    @Operation(summary = "新增请假")
    @PostMapping
    @RepeatSubmit//防止重复提交
    public Result<?> saveDict(@Valid @RequestBody LeaveForm formData) {
        boolean result = leaveService.saveLeave(formData);
        return Result.judge(result);
    }

    @Operation(summary = "修改请假")
    @PutMapping("/{id}")
    //@PreAuthorize("@ss.hasPerm('sys:dict:edit')")
    public Result<?> updateDict(
            @PathVariable Long id,
            @RequestBody LeaveForm leaveForm
    ) {
        boolean status = leaveService.updateLeave(id, leaveForm);
        return Result.judge(status);
    }

    @Operation(summary = "删除请假表单")
    @DeleteMapping("/{ids}")
    //@PreAuthorize("@ss.hasPerm('sys:role:delete')")
    public Result<Void> deleteRoles(
            @Parameter(description = "删除角色，多个以英文逗号(,)拼接") @PathVariable String ids
    ) {
        leaveService.deleteLeaves(ids);
        return Result.success();
    }

}
